package com.yinhe.music.yhmusic.search;

import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.hwangjr.rxbus.RxBus;
import com.yinhe.music.event.RxBusEventType;
import com.yinhe.music.event.RxbusNullObject;
import com.yinhe.music.yhmusic.R;
import com.yinhe.music.yhmusic.base.BaseModel;
import com.yinhe.music.yhmusic.base.BasePresenter;
import com.yinhe.music.yhmusic.base.BaseServiceFragment;
import com.yinhe.music.yhmusic.log.BlankUtil;
import com.yinhe.music.yhmusic.search.adapter.HotSearchAdapter;
import com.yinhe.music.yhmusic.search.adapter.SearchAdapter;
import com.yinhe.music.yhmusic.utils.Preferences;
import com.yinhe.music.yhmusic.widget.SpaceDecoration;
import com.yinhe.music.yhmusic.widget.WrapContentLinearLayoutManager;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * @author 何晓华
 * @date 2017/10/25
 */

public class SearchFragment extends BaseServiceFragment implements View.OnClickListener,SearchContract.ISearchView {

    @BindView(R.id.recommend_music_recyclerview)
    RecyclerView recyclerView;

    private SearchAdapter searchAdapter;
    private List<String> searchList = new ArrayList<>();
    private int[] mBillList = new int[]{1, 2, 100};
    int[] pic = {R.drawable.pic_selected, R.drawable.pic_new, R.drawable.pic_hot};
    private HotSearchAdapter hotSearchAdapter;

    SearchPresenter mPresenter;

    @Override
    public BasePresenter getPresenter() {
        return mPresenter;
    }

    @Override
    public void initPresenter() {
        mPresenter = new SearchPresenter();
        mPresenter.attachModelView(new BaseModel(), this);
    }

    @Override
    public void initView() {
        RxBus.get().register(this);
        initAdapter();
        mPresenter.getHotSearch();
    }

    @Override
    public int getLayoutId() {
        return R.layout.music_hall_recycler_layout;
    }

    @Override
    public void onDestroy() {
        RxBus.get().unregister(this);
        super.onDestroy();
    }

    private void initAdapter() {
        searchList = Preferences.getSearchHistory();
        searchAdapter = new SearchAdapter(R.layout.search_list_item, searchList);
        searchAdapter.addHeaderView(getHotSearchView(getLayoutInflater()));
        searchAdapter.addHeaderView(getHistoryHeadView(getLayoutInflater()));
        searchAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                if (view.getId() == R.id.delete_history_image) {
                    List mData = adapter.getData();
                    if (BlankUtil.isBlank(mData)) {
                        return;
                    }

                    Preferences.removeHistoryItem((String) mData.get(position));
                    searchAdapter.remove(position);
                }
            }
        });

        searchAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                String content = (String) adapter.getData().get(position);
                if (content != null && !TextUtils.isEmpty(content)) {
                    RxBus.get().post(RxBusEventType.Search.SEARCH_FROM_HISTORY, content);
                }
                RxBus.get().post(RxBusEventType.Search.HIDE_SOFT_KEYBOARD, RxbusNullObject.INSTANCE);
            }
        });

        recyclerView.setFocusable(false);
        recyclerView.setLayoutManager(new WrapContentLinearLayoutManager(getActivity()));
        recyclerView.setHasFixedSize(true);
        recyclerView.setAdapter(searchAdapter);

        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if (newState == RecyclerView.SCROLL_STATE_DRAGGING) {
                    RxBus.get().post(RxBusEventType.Search.HIDE_SOFT_KEYBOARD, RxbusNullObject.INSTANCE);
                }
            }
        });

    }

    private View getRankHeadView(LayoutInflater inflater) {
        View view = inflater.inflate(R.layout.search_ranking_header, (ViewGroup) recyclerView.getParent(), false);
        TextView hotRank = view.findViewById(R.id.rank_hot);
        TextView newRank = view.findViewById(R.id.rank_new);
        TextView bestRank = view.findViewById(R.id.rank_best);
        hotRank.setOnClickListener(this);
        newRank.setOnClickListener(this);
        bestRank.setOnClickListener(this);
        return view;
    }

    private View getHotSearchView(LayoutInflater inflater) {
        View view = inflater.inflate(R.layout.search_hot_header, (ViewGroup) recyclerView.getParent(), false);
        RecyclerView hotRecyclerView = view.findViewById(R.id.recyclerView);
        //    private int[] mBillList = new int[]{1, 2, 100};
        //    int[] pic = {R.drawable.pic_selected, R.drawable.pic_new, R.drawable.pic_hot};
        hotSearchAdapter = new HotSearchAdapter(null);
        StaggeredGridLayoutManager gridLayoutManager = new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.HORIZONTAL);
        hotSearchAdapter.setOnItemClickListener((adapter, view1, position) -> {
            String content = (String) adapter.getData().get(position);
            if (content != null && !TextUtils.isEmpty(content)) {
                RxBus.get().post(RxBusEventType.Search.SEARCH_FROM_HISTORY, content);
            }
            RxBus.get().post(RxBusEventType.Search.HIDE_SOFT_KEYBOARD, RxbusNullObject.INSTANCE);
        });
        hotRecyclerView.setLayoutManager(gridLayoutManager);
        hotRecyclerView.setHasFixedSize(true);
        hotRecyclerView.setAdapter(hotSearchAdapter);
        hotRecyclerView.addItemDecoration(new SpaceDecoration(getActivity(), 0, 30, 17, 0));
        return view;
    }

    private View getHistoryHeadView(LayoutInflater inflater) {
        View view = inflater.inflate(R.layout.search_history_header, (ViewGroup) recyclerView.getParent(), false);
        ImageView clear = view.findViewById(R.id.clear_history);
        clear.setOnClickListener(view1 -> {
            Preferences.removeHistory();
            searchList.clear();
            searchAdapter.setNewData(searchList);
        });
        return view;
    }

    @Override
    public void onClick(View view) {
//        Intent intent = new Intent(getActivity(), RankingDetailActivity.class);
//        switch (view.getId()){
//            case R.id.rank_hot:
//                intent.putExtra("type", mBillList[2]);
//                intent.putExtra("pic", pic[2]);
//                startActivity(intent);
//                break;
//            case R.id.rank_best:
//                intent.putExtra("type", mBillList[0]);
//                intent.putExtra("pic", pic[0]);
//                startActivity(intent);
//                break;
//            case R.id.rank_new:
//                intent.putExtra("type", mBillList[1]);
//                intent.putExtra("pic", pic[1]);
//                startActivity(intent);
//                break;
//                default:
//                    break;
//        }
    }

    @Override
    public void setSearchTipsUI(List<String> tipsList) {
        hotSearchAdapter.setNewData(tipsList);
    }
}
